Preview & Edit Marks
Filter Records
Marks Records records
| # | Reg No | Test Score | Exam Score | Total | Grade | Updated By | Created At |
|---|---|---|---|---|---|---|---|
|
|
|
|
|
query("SELECT DISTINCT academic_year FROM calender ORDER BY academic_year DESC"); $years = $stmt_years->fetchAll(PDO::FETCH_ASSOC); // Fetch classes $stmt_classes = $DBcon->query("SELECT DISTINCT classid FROM class ORDER BY classid"); $classes = $stmt_classes->fetchAll(PDO::FETCH_ASSOC); // Fetch subjects $stmt_subjects = $DBcon->query("SELECT subject_id, subjectname FROM subjectss ORDER BY subjectname"); $subjects = $stmt_subjects->fetchAll(PDO::FETCH_ASSOC); } catch (PDOException $e) { $error = "Error fetching dropdown data: " . $e->getMessage(); } // Handle AJAX update request - THIS MUST COME BEFORE ANY OUTPUT if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['action'])) { if ($_POST['action'] == 'update_score') { // Send JSON header header('Content-Type: application/json'); if (!isset($_POST['csrf_token']) || !validate_csrf_token($_POST['csrf_token'])) { echo json_encode(['success' => false, 'message' => 'Invalid CSRF token']); exit; } $sn = intval($_POST['sn']); $field = sanitize_input($_POST['field']); $value = floatval($_POST['value']); // Validate field if (!in_array($field, ['test', 'exam'])) { echo json_encode(['success' => false, 'message' => 'Invalid field']); exit; } // Validate value (0-100) if ($value < 0 || $value > 100) { echo json_encode(['success' => false, 'message' => 'Score must be between 0 and 100']); exit; } try { // Get current values to calculate total $stmt = $DBcon->prepare("SELECT test, exam FROM marks WHERE sn = ?"); $stmt->execute([$sn]); $current = $stmt->fetch(PDO::FETCH_ASSOC); if (!$current) { echo json_encode(['success' => false, 'message' => 'Record not found']); exit; } // Update the score $update_stmt = $DBcon->prepare("UPDATE marks SET $field = ? WHERE sn = ?"); $update_stmt->execute([$value, $sn]); $affected_rows = $update_stmt->rowCount(); if ($affected_rows === 0) { echo json_encode(['success' => false, 'message' => 'No records were updated']); exit; } // Calculate new total if ($field == 'test') { $new_total = $value + $current['exam']; } else { $new_total = $current['test'] + $value; } // Get admin/user from session $admin = isset($_SESSION['username']) ? $_SESSION['username'] : 'System'; echo json_encode([ 'success' => true, 'message' => '✓ Score updated successfully!', 'new_total' => $new_total, 'updated_by' => $admin, 'sn' => $sn, 'field' => $field, 'value' => $value ]); } catch (PDOException $e) { error_log("Update error: " . $e->getMessage()); echo json_encode(['success' => false, 'message' => 'Update failed: ' . $e->getMessage()]); } exit; // IMPORTANT: Stop execution after AJAX response } } // Handle form submission for filtering if ($_SERVER['REQUEST_METHOD'] == 'GET' && isset($_GET['filter'])) { $acyear = sanitize_input($_GET['acyear'] ?? ''); $klass = sanitize_input($_GET['klass'] ?? ''); $subject = sanitize_input($_GET['subject'] ?? ''); $term = sanitize_input($_GET['term'] ?? ''); if (empty($acyear) || empty($klass) || empty($subject) || empty($term)) { $error = "Please select all filter criteria"; } else { try { // Count records $count_stmt = $DBcon->prepare(" SELECT COUNT(*) as count FROM marks WHERE acyear = ? AND klass = ? AND subject = ? AND term = ? "); $count_stmt->execute([$acyear, $klass, $subject, $term]); $result = $count_stmt->fetch(PDO::FETCH_ASSOC); $record_count = $result['count']; if ($record_count > 0) { // Fetch marks records $marks_stmt = $DBcon->prepare(" SELECT m.* FROM marks m WHERE m.acyear = ? AND m.klass = ? AND m.subject = ? AND m.term = ? ORDER BY m.regno "); $marks_stmt->execute([$acyear, $klass, $subject, $term]); $marks_data = $marks_stmt->fetchAll(PDO::FETCH_ASSOC); $message = "Found $record_count record(s)"; } else { $error = "No records found matching the selected criteria."; } } catch (PDOException $e) { $error = "Error fetching records: " . $e->getMessage(); } } } // Generate CSRF token $csrf_token = generate_csrf_token(); ?>
| # | Reg No | Test Score | Exam Score | Total | Grade | Updated By | Created At |
|---|---|---|---|---|---|---|---|
|
|
|
|
|